<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register Page</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background-color: #202123;
            color: #FFFFFF;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            text-align: center;
            background-color: #33373E;
            padding: 40px 50px;
            border-radius: 15px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
            width: 100%;
            max-width: 400px;
        }

        h1 {
            margin-bottom: 20px;
            font-size: 24px;
        }

        form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        input {
            background-color: #202123;
            color: #FFFFFF;
            border: 1px solid #444951;
            padding: 10px 15px;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        input:focus {
            border-color: #10A37F;
            outline: none;
        }

        button {
            background-color: #10A37F;
            color: #FFFFFF;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0E8F6E;
        }

        .footer {
            margin-top: 20px;
            font-size: 14px;
            color: #B0B3B8;
        }

        .footer a {
            color: #10A37F;
            text-decoration: none;
            transition: color 0.3s;
        }

        .footer a:hover {
            color: #0E8F6E;
        }

        .message {
            color: green;
            margin-top: 20px;
            display: none;
        }

        .error-message {
            color: red;
            margin-top: 20px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Create Your Account</h1>
        <form id="registerForm">
            <input type="text" id="username" placeholder="Username" required>
            <input type="password" id="password" placeholder="Password" required>
            <button type="submit">Register</button>
        </form>
        <div class="footer">
            <p>Already have an account? <a href="/login">Log in</a></p>
        </div>
        <div id="errorMessage" class="error-message"></div>
        <div id="successMessage" class="message"></div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 确保页面加载时隐藏错误消息
            document.getElementById('errorMessage').style.display = 'none';
            document.getElementById('successMessage').style.display = 'none';
        });

        document.getElementById('registerForm').addEventListener('submit', function(event) {
            event.preventDefault(); // 阻止表单的默认提交行为

            var username = document.getElementById('username').value;
            var password = document.getElementById('password').value;

            var data = {
                username: username,
                password: password
            };

            fetch('register.lua', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            })
            .then(response => {
                if (!response.ok) {
                    return response.json().then(data => {
                        throw new Error(data.message || 'Network response was not ok');
                    });
                }
                return response.json();
            })
            .then(data => {
                if (data.success) {
                    // 清空表单数据
                    document.getElementById('username').value = '';
                    document.getElementById('password').value = '';

                    // 弹窗提示注册成功
                    alert(data.message || "Registration successful!");
                    
                    document.getElementById('errorMessage').innerText = '';
                    // 立即跳转到登录页面
                    window.location.href = '/login';
                } else {
                    // 显示错误消息
                    document.getElementById('errorMessage').innerText = data.message || "Registration failed. Please try again.";
                    document.getElementById('errorMessage').style.display = 'block';
                }
            })
            .catch(error => {
                console.error('Error:', error);
                document.getElementById('errorMessage').innerText = error.message || "An unknown error occurred. Please try again.";
                document.getElementById('errorMessage').style.display = 'block';
            });
        });
    </script>
</body>
</html>